/**
 * Created by Administrator on 2015/12/29.
 */
"use strict";
var F = require('../../../common/function');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');
_.v = require('validator');
var coupon_F = require('./coupon_common');

function mCoupon(couponInfo) {
  this.set(couponInfo);
}

mCoupon.prototype.set = function (couponInfo) {
  this.couponInfo = {
    id: parseInt(couponInfo['id']) || 0,
    name: couponInfo['name'] || '',
    vendorid: couponInfo['vendorid'] || 0,
    money: couponInfo['money'] || 0,
    circulation: couponInfo['circulation'] || 0,
    applicableGte: couponInfo['applicableGte'] || 0,
    applicableUser: couponInfo['applicableUser'] || 0,
    onLineTime: couponInfo['onLineTime'] || null,
    offLineTtime: couponInfo['offLineTtime'] || null,
    type: couponInfo['type'] || 0,
    provide: couponInfo['provide'] || 0,
    valid: couponInfo['valid'] || 0,
    status: couponInfo['status'] || 0,
    uid: couponInfo['uid'] || 0
  };
};

mCoupon.prototype.get = function () {
  return this.couponInfo;
};
let key_validate_array = ['name', 'money', 'circulation', 'applicableGte', 'applicableUser', 'onLineTime', 'offLineTtime'];

mCoupon.prototype.add = function *() {
  let error_msg = coupon_F.validateFields(this.couponInfo, key_validate_array);
  if (error_msg != '') {
    return F.returnMsg(400, error_msg, 1);
  }
  let obj_coupon = _.clone(this.couponInfo);
  obj_coupon.act = 1;
  let coupon_result = yield o2oServiceApi.post('coupon', 'add', 1, obj_coupon);
  if (parseInt(coupon_result['result']['error']) == 1 || coupon_result.status == 400) {
    return F.returnMsg(300, coupon_result['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '成功', 1, coupon_result['result']);
  }
};

mCoupon.prototype.update = function *() {
  key_validate_array = _.union(key_validate_array, ['provide', 'valid', 'status', 'id']);
  let obj_coupon_for_update = {};
  let keys_for_update_array = [];
  for (let p of key_validate_array) {
    if (this.couponInfo[p] != undefined) {
      obj_coupon_for_update[p] = this.couponInfo[p];
      keys_for_update_array.push(p);
    }
  }
  let error_msg = coupon_F.validateFields(obj_coupon_for_update, keys_for_update_array);
  if (error_msg != '') {
    return F.returnMsg(400, error_msg, 1);
  }
  obj_coupon_for_update.act = 1;
  console.log(obj_coupon_for_update);
  let couponResult = yield o2oServiceApi.post('coupon', 'update', 1, obj_coupon_for_update);
  if (parseInt(couponResult['result']['error']) == 1 || couponResult.status == 400) {
    return F.returnMsg(300, couponResult['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '保存成功', 1, couponResult['result']);
  }
};

mCoupon.prototype.detail = function *() {
  let obj_coupon_query = _.clone(this.couponInfo);
  obj_coupon_query.act = 1;
  let couponResult = yield o2oServiceApi.post('coupon', 'detail', 1, obj_coupon_query);
  if (parseInt(couponResult['result']['error']) == 1 || couponResult.status == 400) {
    return F.returnMsg(300, couponResult['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '成功', 1, couponResult['result']);
  }
};

mCoupon.prototype.addLog = function *() {
  let coupon_log_info = _.pick(this.couponInfo, 'id', 'uid');
  coupon_log_info.couponid = coupon_log_info.id;
  if (coupon_log_info.uid == undefined || !_.v.isInt(coupon_log_info.uid)) {
    return F.returnMsg(400, 'uid参数无效，请检查', 1);
  }
  coupon_log_info.act = 1;
  let user_info = yield F.returnUserInfo(coupon_log_info.uid);
  if (user_info && user_info.length > 0) {
    coupon_log_info.city = user_info[0].city;
  } else {
    return F.returnMsg(400, '未能找到与uid匹配的用户', 1);
  }
  let couponResult = yield o2oServiceApi.post('coupon', 'addLog', 1, coupon_log_info);
  if (parseInt(couponResult['result']['error']) == 1 || couponResult.status == 400) {
    return F.returnMsg(300, couponResult['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '领取成功', 1, couponResult['result']);
  }
};

mCoupon.prototype.updateLog = function *() {
  let coupon_log_info = _.clone(this.couponInfo);
  if (!_.v.isIn(coupon_log_info.status, [1, 2])) {
    return F.returnMsg(400, 'status字段只能传入1（已领取）或2（已使用）', 1);
  }
  coupon_log_info.act = 1;
  let couponResult = yield o2oServiceApi.post('coupon', 'updateLog', 1, coupon_log_info);
  if (parseInt(couponResult['result']['error']) == 1 || couponResult.status == 400) {
    return F.returnMsg(300, couponResult['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '更新成功', 1, couponResult['result']);
  }
};

mCoupon.prototype.logQuery = function *() {
  let obj_query = {act: 1, isPaging: 1, page: 1, pageSize: 30};
  if (!!this.couponInfo.page && _.v.isInt(this.couponInfo.page)) {
    obj_query.page = parseInt(this.couponInfo.page);
  }
  if (!!this.couponInfo.pageSize && _.v.isInt(this.couponInfo.pageSize)) {
    obj_query.pageSize = parseInt(this.couponInfo.pageSize);
  }
  if(!!this.couponInfo.id && _.v.isInt(this.couponInfo.id) && this.couponInfo.id>0 ){
      obj_query.couponid = this.couponInfo.id;
      obj_query.act =2;
  }else{
     delete this.couponInfo.id;
  }

  if (!!this.couponInfo.vendorid && _.v.isInt(this.couponInfo.vendorid)) {
    obj_query.vendorid = parseInt(this.couponInfo.vendorid);
  }else{
    delete this.couponInfo.vendorid;
  }
  let couponResult = yield o2oServiceApi.post('coupon', 'listLog', 1, obj_query);
  if (parseInt(couponResult['result']['error']) == 1 || couponResult.status == 400) {
    return F.returnMsg(300, couponResult['message']['msg'], 1);
  } else {
    if (couponResult['result']['list'].length == 0 || _.isEmpty(couponResult['result']['list'])) {
      return F.returnMsg(200, '没有数据', 1);
    }
    if(!!this.couponInfo.vendorid && !!this.couponInfo.uid && this.couponInfo.uid>0){
      let uid = this.couponInfo.uid;
      couponResult['result']['list'] = _.filter(couponResult['result']['list'],function(obj){
        return  obj.uid == uid;
      });
      couponResult['result']['count'] = couponResult['result']['list'].length;
    }
    return F.returnMsg(200, '成功', 1, couponResult['result']);
  }
};

mCoupon.prototype.logDetail = function *() {
  let id = this.couponInfo.id;
  if (id == undefined || !_.v.isInt(id)) {
    return F.returnMsg(400, 'id字段无效', 1);
  }
  let couponlogResult = yield o2oServiceApi.post('coupon', 'detailLog', 1, {act: 1, id: this.couponInfo.id});
  if (parseInt(couponlogResult['result']['error']) == 1 || couponlogResult.status == 400) {
    return F.returnMsg(300, couponlogResult['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '成功', 1, couponlogResult['result']);
  }
};

mCoupon.prototype.query = function *() {
  let obj_query = {act: 2, isPaging: 1};
  obj_query.type = parseInt(this.couponInfo.type);

  let vendorid = this.couponInfo.vendorid;
  if (vendorid == undefined || !_.v.isInt(vendorid)) {
    return F.returnMsg(400, '请输入有效的vendorid', 1);
  }
  obj_query.vendorid = vendorid;

  let query = this.couponInfo.query;
  if (query != undefined && !_.isEmpty(query)) {
    obj_query.query = query;
  }

  let onLineTime = this.couponInfo.onLineTime;
  if (onLineTime != undefined && _.v.isDate(onLineTime)) {
    onLineTime = onLineTime.format("yyyy-MM-dd");
    onLineTime = parseInt(onLineTime.replace('-', ''));
    obj_query.onLineTime = onLineTime;
  }

  let offLineTime = this.couponInfo.offLineTime;
  if (offLineTime != undefined && _.v.isDate(offLineTime)) {
    offLineTime = offLineTime.format("yyyy-MM-dd");
    offLineTime = parseInt(offLineTime.replace('-', ''));
    obj_query.offLineTime = offLineTime;
  }
  let coupon_result = yield o2oServiceApi.post('coupon', 'list', 1, obj_query);
  if (parseInt(coupon_result['result']['error']) == 1 || coupon_result.status == 400) {
    return F.returnMsg(300, coupon_result['message']['msg'], 1);
  } else {
    if (coupon_result['result']['list'].length == 0 || _.isEmpty(coupon_result['result']['list'])) {
      return F.returnMsg(200, '没有数据', 1);
    }
    return F.returnMsg(200, '成功', 1, coupon_result['result']);
  }
};

exports.mCoupon = mCoupon;